/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DBPane.java
 *
 * Created on Aug 17, 2009, 10:33:26 AM
 */
package org.dbdir.client.gui;

import java.sql.Timestamp;
import javax.swing.JOptionPane;
import org.dbdir.Client;
import org.dbdir.core.helper.ACL;
import org.dbdir.core.helper.Database;

/**
 *
 * @author thotheolh
 */
public class DBPane extends javax.swing.JPanel {

    Client client;
    MainFrame frame;
    Database db;
    ACL acl;

    /** Creates new form DBPane */
    public DBPane(Client client, MainFrame frame) {
        initComponents();
        this.client = client;
        this.frame = frame;
        this.setSize(400, 222);
    }

    private void adminFunctions() {
        if (client.isAdmin()) {
            memBtn.setEnabled(true);
            killDBBtn.setEnabled(true);
        } else {
        }
    }

    public void init() {
        db = client.getDB(client.getCurrentDBProfile());
        acl = client.getUserACL();
        String alias = db.getDbAlias();
        String path = db.getDbPath();
        String dbusr = db.getDbUsername();
        String dbpwd = db.getDbPassword();
        Timestamp datetime = db.getDatetimeResgistered();

        if (alias != null) {
            aliasTF.setText(db.getDbAlias());
        }
        if (path != null) {
            pathTF.setText(db.getDbPath());
        }
        if (dbusr != null) {
            usrTF.setText(db.getDbUsername());
        }
        if (dbpwd != null) {
            pwdTF.setText(db.getDbPassword());
        }
        if (datetime != null) {
            dtTF.setText(datetime.toString());
        }
        if (acl.isEditDbPath()) {
            pathTF.setEnabled(true);
            proceedBtn.setEnabled(true);
        }
        if (acl.isEditDbUsername()) {
            usrTF.setEnabled(true);
            proceedBtn.setEnabled(true);
        }
        if (acl.isEditDbPassword()) {
            pwdTF.setEnabled(true);
            proceedBtn.setEnabled(true);
        }

        adminFunctions();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        contentPane = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        aliasTF = new javax.swing.JTextField();
        pathTF = new javax.swing.JTextField();
        usrTF = new javax.swing.JTextField();
        pwdTF = new javax.swing.JTextField();
        dtTF = new javax.swing.JTextField();
        bottomPane = new javax.swing.JPanel();
        leftPane = new javax.swing.JPanel();
        mainBtn = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        rightPane = new javax.swing.JPanel();
        killDBBtn = new javax.swing.JButton();
        memBtn = new javax.swing.JButton();
        proceedBtn = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jLabel1.setText("DB Alias: ");

        jLabel2.setText("DB Path: ");

        jLabel3.setText("DB Username: ");

        jLabel4.setText("DB Password: ");

        jLabel5.setText("DateTime Registered: ");

        aliasTF.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        aliasTF.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        aliasTF.setEnabled(false);
        aliasTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aliasTFActionPerformed(evt);
            }
        });

        pathTF.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        pathTF.setEnabled(false);

        usrTF.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        usrTF.setEnabled(false);

        pwdTF.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        pwdTF.setEnabled(false);

        dtTF.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        dtTF.setEnabled(false);

        javax.swing.GroupLayout contentPaneLayout = new javax.swing.GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(contentPaneLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(aliasTF, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(contentPaneLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pathTF, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, contentPaneLayout.createSequentialGroup()
                            .addComponent(jLabel5)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(dtTF))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, contentPaneLayout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addGap(18, 18, 18)
                            .addComponent(pwdTF))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, contentPaneLayout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(usrTF, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(21, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(aliasTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(pathTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(usrTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(pwdTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(dtTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        add(contentPane, java.awt.BorderLayout.CENTER);

        bottomPane.setLayout(new java.awt.BorderLayout());

        leftPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        mainBtn.setText("Main");
        mainBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mainBtnActionPerformed(evt);
            }
        });
        leftPane.add(mainBtn);

        jButton1.setText("DB");
        jButton1.setToolTipText("Manage DB page");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        leftPane.add(jButton1);

        bottomPane.add(leftPane, java.awt.BorderLayout.WEST);

        rightPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        killDBBtn.setText("Kill DB");
        killDBBtn.setToolTipText("Destroy current DB Profile");
        killDBBtn.setEnabled(false);
        killDBBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                killDBBtnActionPerformed(evt);
            }
        });
        rightPane.add(killDBBtn);

        memBtn.setText("Members");
        memBtn.setToolTipText("Manage Members for Admin");
        memBtn.setEnabled(false);
        memBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                memBtnActionPerformed(evt);
            }
        });
        rightPane.add(memBtn);

        proceedBtn.setText("Update");
        proceedBtn.setToolTipText("Update DB Profile properties");
        proceedBtn.setEnabled(false);
        proceedBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                proceedBtnActionPerformed(evt);
            }
        });
        rightPane.add(proceedBtn);

        bottomPane.add(rightPane, java.awt.BorderLayout.EAST);

        add(bottomPane, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    private void mainBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mainBtnActionPerformed
        // TODO add your handling code here:
        frame.getActionPane();
}//GEN-LAST:event_mainBtnActionPerformed

    private void proceedBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_proceedBtnActionPerformed
        // TODO add your handling code here:
        Database db = new Database(client.getCurrentDBProfile(), pathTF.getText(), usrTF.getText(), pwdTF.getText(), null);
        if (client.updateDBProfile(db)) {
            JOptionPane.showMessageDialog(null, "DB profile have successfully been updated.", "DB Profile Updated", JOptionPane.INFORMATION_MESSAGE);
            client.getFrame().getManDBPane();
        }else{
            JOptionPane.showMessageDialog(null, "DB profile have failed to beupdated.", "Update DB Profile Failed", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_proceedBtnActionPerformed

    private void aliasTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aliasTFActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_aliasTFActionPerformed

    private void killDBBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_killDBBtnActionPerformed
        // TODO add your handling code here:
        int n = JOptionPane.showConfirmDialog(
                null,
                "Are you sure you want to destroy this DB profile ?",
                "Destroy DB Profile",
                JOptionPane.YES_NO_OPTION);
        if (n == 0) {
            if (client.delCurrentDB()) {
                JOptionPane.showMessageDialog(null, "The specific DB profile have been destroyed !", "DB Profile Successfully Destroyed", JOptionPane.INFORMATION_MESSAGE);
                frame.getManDBPane();
            } else {
                JOptionPane.showMessageDialog(null, "The specific DB profile have failed to be destroyed !", "DB Profile Failed Destroyed", JOptionPane.ERROR_MESSAGE);
            }
        }
}//GEN-LAST:event_killDBBtnActionPerformed

    private void memBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_memBtnActionPerformed
        // TODO add your handling code here:
        frame.getMemPane();
    }//GEN-LAST:event_memBtnActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        frame.getManDBPane();
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField aliasTF;
    private javax.swing.JPanel bottomPane;
    private javax.swing.JPanel contentPane;
    private javax.swing.JTextField dtTF;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JButton killDBBtn;
    private javax.swing.JPanel leftPane;
    private javax.swing.JButton mainBtn;
    private javax.swing.JButton memBtn;
    private javax.swing.JTextField pathTF;
    private javax.swing.JButton proceedBtn;
    private javax.swing.JTextField pwdTF;
    private javax.swing.JPanel rightPane;
    private javax.swing.JTextField usrTF;
    // End of variables declaration//GEN-END:variables
}
